﻿using System.Collections.Generic;

using ARCHFORME.Common;

namespace ARCHFORME.Core
{
    public class CommandService
    {
        private readonly Dictionary<ICommand, IHandleCommand<ICommand>> handlerRegistry;

        public CommandService(Dictionary<ICommand, IHandleCommand<ICommand>> handlerRegistry)
        {
            this.handlerRegistry = handlerRegistry;
        }

        public void Execute(ICommand command)
        {
            AssertHandlerExistsForCommand(command);

            handlerRegistry[command].Handle(command);
        }

        private void AssertHandlerExistsForCommand(ICommand command)
        {
            if (!handlerRegistry.ContainsKey(command))
            {
                throw new NoHandlerRegisteredForCommandException("For command: " + command.GetType().Name);
            }
        }
    }
}